package com.bw.module_login.view

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.os.CountDownTimer
import android.util.Log
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.bw.module_login.vm.LoginViewModel
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.module_login.R
import com.bw.module_login.databinding.ActivityLoginBinding
import kotlin.random.Random

@Route(path = RouterPath.MODULE_LOGIN_ACTIVITY)
class LoginActivity : BaseMvvmActivity<ActivityLoginBinding>() {

    private val vm: LoginViewModel by viewModels()
    lateinit var mValueAnimator: ValueAnimator
    override fun initViews() {
        binding.loginLayout.login.setOnClickListener {
            if(binding.loginLayout.etUsername.text.toString().isEmpty()||binding.loginLayout.etPassword.text.toString().isEmpty()){
                ToastUtils.showLong("用户名或密码不能为空")
                return@setOnClickListener
            }
            vm.loginToServer(binding.loginLayout.etPassword.text.toString(),binding.loginLayout.etUsername.text.toString())
        }
        binding.loginLayout.goRegister.setOnClickListener {
            binding.registerLayout.registerView.visibility = View.VISIBLE
            binding.loginLayout.loginView.visibility = View.GONE
        }
//        binding.userTopBar.topBarIcon.visibility = View.GONE
//        binding.userTopBar.topBarWhiteNotice.visibility = View.GONE
        binding.registerLayout.register.setOnClickListener {
            if(binding.registerLayout.etPassword.text.toString()!=binding.registerLayout.etRePassword.text.toString()){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
            if(binding.registerLayout.etPhone.text.toString().isEmpty()||binding.registerLayout.etPassword.text.toString().isEmpty()||binding.registerLayout.etRePassword.text.toString().isEmpty()){
                ToastUtils.showLong("用户名或密码不能为空")
                return@setOnClickListener
            }
            vm.registerToServer(binding.registerLayout.etPhone.text.toString(),binding.registerLayout.etRePassword.text.toString())
        }
        binding.registerLayout.goLogin.setOnClickListener {
            binding.registerLayout.registerView.visibility = View.GONE
            binding.loginLayout.loginView.visibility = View.VISIBLE
        }
        binding.registerLayout.btnCode.setOnClickListener{

            if(!RegexUtils.isMobileExact(binding.registerLayout.etPhone.text.toString())){
                ToastUtils.showLong("请输入正确的手机号")
                return@setOnClickListener
            }
            val code = (100000..999999).random().toString()
            ToastUtils.showLong(code)
            binding.registerLayout.etCode.setText(code)
            binding.registerLayout.btnCode.isClickable = false
            binding.registerLayout.etCode.setText(code)
            mValueAnimator = ValueAnimator.ofInt(15,0)
            mValueAnimator.duration = 15000
            mValueAnimator.interpolator = LinearInterpolator()
            mValueAnimator.addUpdateListener {
                val count = it.animatedValue as Int
                binding.registerLayout.btnCode.text = "${count}秒后重试"
            }
            mValueAnimator.start()
            mValueAnimator.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    binding.registerLayout.btnCode.text = "获取验证码"
                    binding.registerLayout.btnCode.isClickable = true
                }
            })
        }
    }

    override fun loadData(){

        vm.mErrorLiveData.observe(this@LoginActivity){
            showToast(it)
            Log.i("sss", "loadData: $it")
        }

        vm.mSuccessLiveData.observe(this@LoginActivity){
            if(it != null){
                showToast("登录成功")
                //跳转到主页面
                ARouter.getInstance().build(RouterPath.MODULE_MAIN_ACTIVITY).navigation()
            }
        }
    }

    override fun observeDataChange() {}

    override fun getLayoutId(): Int = R.layout.activity_login

}